package com.hk.employeemanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.employeemanagementsystem.entity.PositionEntity;
import com.hk.employeemanagementsystem.mapper.PositionEntityMapper;
import com.hk.employeemanagementsystem.service.PositionEntityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author by 何坤
 * @Classname PositionEntityServiceImpl
 * @Description TODO
 * @Date 2020/1/7 22:48
 */
@Service
public class PositionEntityServiceImpl extends ServiceImpl<PositionEntityMapper, PositionEntity> implements PositionEntityService {
    private Logger logger = LoggerFactory.getLogger(PositionEntityServiceImpl.class);

    @Resource
    private RedisTemplate<String, PositionEntity> redisTemplate;

    @Resource
    private PositionEntityMapper positionEntityMapper;

    @Cacheable(value = "position", key = "'all'", unless = "#result==null || #result.size()==0")
    @Override
    public List<PositionEntity> selectAllPosition() {
//        return positionEntityMapper.selectList(new QueryWrapper<PositionEntity>().eq("pos_enabled", true));
        return positionEntityMapper.selectList(null);
    }

    @CachePut(value = "position", key = "'all'", unless = "#result==null || #result.size()==0")
    @Override
    public List<PositionEntity> updatePosition(PositionEntity positionEntity) {
        positionEntityMapper.updateById(positionEntity);
        return positionEntityMapper.selectList(null);
    }

    @CachePut(value = "position", key = "'all'", unless = "#result==null || #result.size()==0")
    @Override
    public List<PositionEntity> deletePosition(List<String> ids) {
        positionEntityMapper.deleteBatchIds(ids);
        return positionEntityMapper.selectList(null);
    }

    @CachePut(value = "position", key = "'all'", unless = "#result==null || #result.size()==0")
    @Override
    public List<PositionEntity> savePosition(PositionEntity positionEntity) {
        positionEntityMapper.insert(positionEntity);
        return positionEntityMapper.selectList(null);
    }

    @Override
    public Boolean validatePositionName(String name, Integer id) {
        if (id == null){
            return positionEntityMapper.selectOne(new QueryWrapper<PositionEntity>().eq("pos_name", name)) == null ? false : true;
        } else {
            return positionEntityMapper.selectOne(new QueryWrapper<PositionEntity>().eq("pos_name", name).ne("pos_id", id)) == null ? false : true;
        }
    }
}
